package com.wzapp.sitposture.eshop.endpoint;

import com.wzapp.sitposture.eshop.pay.properties.AlipayProperties;
import com.wzapp.sitposture.eshop.pay.properties.WeChatPaymentProperties;
import com.icanpay.Merchant;
import com.icanpay.enums.GatewayType;
import com.icanpay.enums.PaymentNotifyMethod;
import com.icanpay.events.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/notify")
@Deprecated
public class NotifyEndpoint {
	private static final Logger log = LoggerFactory.getLogger(NotifyEndpoint.class);

	List<Merchant> merchantList;

	PaymentNotify notify;

	private AlipayProperties alipayProperties;

	private WeChatPaymentProperties weChatPaymentProperties;


	@Autowired
	public NotifyEndpoint(AlipayProperties alipayProperties,
						  WeChatPaymentProperties weChatPaymentProperties) {
		this.alipayProperties = alipayProperties;
		this.weChatPaymentProperties = weChatPaymentProperties;

		merchantList = new ArrayList<Merchant>();

		Merchant alipayMerchant = new Merchant();
		alipayMerchant.setGatewayType(GatewayType.Alipay);
		alipayMerchant.setAppId(this.alipayProperties.getAppid());
		alipayMerchant.setEmail(this.alipayProperties.getSeller_email());
		alipayMerchant.setPartner(this.alipayProperties.getPartner());
		alipayMerchant.setKey(this.alipayProperties.getKey());
		alipayMerchant.setPrivateKeyPem(this.alipayProperties.getPrivatekeypem());
		alipayMerchant.setPublicKeyPem(this.alipayProperties.getPublicKeypem());

		Merchant weChatPaymentMerchant = new Merchant();
		weChatPaymentMerchant.setGatewayType(GatewayType.WeChatPayment);
		weChatPaymentMerchant.setAppId(this.weChatPaymentProperties.getAppid());
		weChatPaymentMerchant.setPartner(this.weChatPaymentProperties
				.getMch_id());
		weChatPaymentMerchant.setKey(this.weChatPaymentProperties.getKey());

		merchantList.add(alipayMerchant);
		merchantList.add(weChatPaymentMerchant);

		// 添加到商户数据集合
		notify = new PaymentNotify(merchantList);

		notify.setPaymentSucceed(new PaymentSucceedListener() {

			@Override
			public void handleEvent(PaymentSucceedEventArgs event) {
				// TODO Auto-generated method stub
				// 支付成功时时的处理代码
				if (event.getPaymentNotifyMethod() == PaymentNotifyMethod.AutoReturn) {
					// 当前是用户的浏览器自动返回时显示充值成功页面
				} else {
					// 支付结果的发送方式，以服务端接收为准
				}
			}

		});

		notify.setPaymentFailed(new PaymentFailedListener() {

			@Override
			public void handleEvent(PaymentFailedEventArgs event) {
				// TODO Auto-generated method stub

			}

		});

		notify.setUnknownGateway(new UnknownGatewayListener() {

			@Override
			public void handleEvent(UnknownGatewayEventArgs event) {
				// TODO Auto-generated method stub

			}
		});

	}

	@GetMapping(value = "/servernotify-bak", produces="text/html;charset=utf-8")
	public void ServerNotify() throws Exception {

		log.info("===============ServerNotify================");

		// 接收并处理支付通知
		notify.received(PaymentNotifyMethod.ServerNotify);
//		return RestfulResult.ok("success");
	}

	@GetMapping("/autoreturn")
	public void AutoReturn() throws Exception {
		// 接收并处理支付通知
		notify.received(PaymentNotifyMethod.AutoReturn);
	}

	/**
	 * 异步回调接口
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/servernotify",produces="text/html;charset=utf-8")
	@ResponseBody
    @Async
	public void WeixinParentNotifyPage(HttpServletRequest request, HttpServletResponse response) throws Exception{

		log.info("===============ServerNotify==new==============");

        log.info("微信回调接口结束====================");
    }


}
